package com.jrh.web.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jrh.biz.brand.BrandService;
import com.jrh.biz.img.ImgUploadService;
import com.jrh.biz.mall.MallService;
import com.jrh.biz.user.UserService;
import com.jrh.common.exception.UserAlreadyExistsException;
import com.jrh.dal.LabelDao;
import com.jrh.dal.MallBrandDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Brand;
import com.jrh.domain.entity.Label;
import com.jrh.domain.entity.Mall;
import com.jrh.domain.entity.MallBrand;
import com.jrh.domain.entity.User;

/**
 * 后台品牌相关控制器
 * 
 * @author yout 2012-7-22 上午11:16:12
 */
@Controller
@RequestMapping("/admin/brand")
public class AdminBrandController {

    private static final String  UPLOAD_TYPE = "brand";
    private static final Integer pageSize    = 5;

    @Autowired
    private BrandService         brandService;

    @Autowired
    private MallBrandDao         mallBrandDao;

    @Autowired
    private MallService          mallService;

    @Autowired
    private ImgUploadService     imgUploadService;

    @Autowired
    private UserService          userService;

    @Autowired
    private LabelDao             labelDao;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addView(String success, Model model) {
        List<Label> labelList = labelDao.listChildLabel(Label.LABEL_STYLE_KEY);
        model.addAttribute("labelList", labelList);
        model.addAttribute("success", success);
        return "admin/brand/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Brand brand, MultipartFile coverImg, String userName, String password, Model model) {
        String coverImgPath = imgUploadService.upload(coverImg, UPLOAD_TYPE);
        brand.setLogoPic(coverImgPath);

        User user = new User();
        user.setNickname(userName);
        user.setLoginId(userName);
        user.setPassword(password);
        user.setUserType(User.USER_TYPE_SELLER);
        try {
            userService.createUser(user, null);
        } catch (UserAlreadyExistsException e) {
            model.addAttribute("msg", "用户重复");
            return "/admin/error";
        }
        brand.setUserId(user.getId());
        brandService.addBrand(brand);
        return "redirect:/admin/brand/add.htm?success=" + Boolean.TRUE.toString();
    }

    @RequestMapping("/list")
    public String list(Integer pageIndex, Model model) {
        Brand brand = new Brand();
        if (pageIndex == null) {
            pageIndex = 1;
        }
        Integer start = (pageIndex - 1) * pageSize;
        Page<Brand> list = brandService.listBrandsByCondition(brand, start, pageSize, null);
        model.addAttribute("list", list);
        return "admin/brand/list";
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String updateView(@PathVariable("id") long id, Model model, String success) {
        List<Label> labelList = labelDao.listChildLabel(Label.LABEL_STYLE_KEY);
        model.addAttribute("labelList", labelList);
        Brand brand = brandService.getBrandById(id);
        model.addAttribute("brand", brand);
        model.addAttribute("success", Boolean.valueOf(success));
        return "admin/brand/update";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Brand brand, Model model, MultipartFile coverImg) {

        if (coverImg != null && !coverImg.isEmpty()) {
            String coverImgPath = imgUploadService.upload(coverImg, UPLOAD_TYPE);
            brand.setLogoPic(coverImgPath);
        }
        brandService.modifyBrand(brand);
        return "redirect:/admin/brand/update/" + brand.getId() + "?success=" + Boolean.TRUE.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable("id") long id) {
        try {
            brandService.delete(id);
            return Boolean.TRUE.toString();
        } catch (Exception e) {
            return Boolean.FALSE.toString();
        }
    }

    @RequestMapping("/mall/{brandId}")
    public String mall(@PathVariable("brandId") long brandId, Model model) {
        Brand brand = brandService.getBrandById(brandId);
        if (brand == null) {
            return "admin/error";
        }
        List<MallBrand> list = mallBrandDao.listBrandMall(brandId);
        List<Mall> mallList = mallService.findAll();
        model.addAttribute("list", list);
        model.addAttribute("mallList", mallList);
        model.addAttribute("brandId", brandId);
        model.addAttribute("brand", brand);
        return "admin/brand/mallBrand";
    }

    @RequestMapping("/mall/add")
    public String addMallBrand(MallBrand mallBrand) {
        mallBrandDao.create(mallBrand);
        return "redirect:/admin/brand/mall/" + mallBrand.getBrandId();
    }

    @ResponseBody
    @RequestMapping(value = "/mall/delete/{id}", method = RequestMethod.GET)
    public String deleteMallBrand(@PathVariable("id") long id) {
        try {
            mallBrandDao.delete(id);
            return Boolean.TRUE.toString();
        } catch (Exception e) {
            return Boolean.FALSE.toString();
        }
    }
}
